<?php
require_once  __DIR__ . '/includes/authenticate.php';
include_once __DIR__ . '/db_bb_connect.php';

//ini_set('display_errors',1);
//ini_set('display_startup_errors',1);
//error_reporting(-1);

require_once __DIR__ . '/includes/BasketbalFunctions.php';

$basketbalfunctions = new BasketbalFunctions();

?>

<div class="container-fluid">

<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<h1>Player Home Viewer</h1>

<form method="POST" action="">
    <div class="form-group">
    <div class="col-md-offset-3 col-md-6">        
          <select class="form-control" name="playerID">
          <option value="" disabled="disabled" selected="selected">Select a player</option>

<?php

$sql = "SELECT firstname, lastname, user_key, type
FROM users 
WHERE type = 'Player'
ORDER BY firstname";

$result=$db->query($sql);

while($row=$result->fetch_assoc()){ 

  $user_key=$row['user_key']; 
  $firstname=$row['firstname']; 
  $lastname=$row['lastname']; 
  
  echo "<option value='$user_key'>$firstname $lastname</option>";

}

  ?>

  </select>
</div>
</div>
</form>

<div id="profile"></div>

</div>

<div class="col-md-1"></div>
</div>
</div>

<?php if( isset($_POST['playerID']) ) { 

 $playerID = $_POST['playerID'];


 ?>

 <script type="text/javascript">
  
function loadPlayerHome(e) {
  $.ajax({
    type: "POST",
    url: "./home_player.php",
    data: { playerID: "<?= $playerID; ?>"},
    success: function(data) {
      $("#profile").html(data);
    },
    error: function (xhr, status, error) {
          alert(xhr.responseText);
        }
  }); 
  e.preventDefault();
};

loadPlayerHome();


</script>

<?php } ?>

<script type="text/javascript">
  
$("select").on( "change", function( e ) {
  postData = $(this).serialize();
  $.ajax({
    type: "POST",
    url: "./home_player_admin.php",
    data: postData,
    success: function(data) {
      $("#content").html(data);
    }
  }); 
  e.preventDefault();
});


</script>